package com.atos.optical.service.impl;

import cn.hutool.core.util.StrUtil;
import com.atos.optical.common.BaseServiceImpl;
import com.atos.optical.common.ServiceException;
import com.atos.optical.mapper.SysProductInfoMapper;
import com.atos.optical.mapper.SysProductInfoVersionMapper;
import com.atos.optical.model.po.SysProductInfo;
import com.atos.optical.model.po.SysProductInfoVersion;
import com.atos.optical.model.query.SysProductInfoQuery;
import com.atos.optical.service.SysProductInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * @author wanghra
 * @version 1.0
 * @description
 * @date 2025/8/15
 */
@Slf4j
@Service
public class SysProductInfoServiceImpl extends BaseServiceImpl<SysProductInfoMapper,
        SysProductInfo> implements SysProductInfoService {
    @Autowired
    private SysProductInfoMapper mapper;
    @Autowired
    private SysProductInfoVersionMapper versionMapper;

    @Override
    public boolean save(SysProductInfo entity) {
        check(entity);
        return super.save(entity);
    }

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        // 删除产品信息时同步删除历史版本信息
        QueryWrapper<SysProductInfoVersion> versionQuery = new QueryWrapper<>();
        versionQuery.lambda().eq(SysProductInfoVersion::getProductId, id);
        versionMapper.delete(versionQuery);
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean update(String userName, SysProductInfo entity) {
        check(entity);
        // 版本号+1，并生成历史版本信息
        String oldVersion = entity.getVersion();
        String version = (Integer.parseInt(oldVersion.split("\\.")[0]) + 1) + ".0";
        entity.setVersion(version);
        SysProductInfoVersion infoVersion = new SysProductInfoVersion();
        infoVersion.setProductId(entity.getProductId())
                .setTypeId(entity.getTypeId())
                .setProductPrice(entity.getProductPrice())
                .setProductNum(entity.getProductNum())
                .setVersion(oldVersion)
                .setUserName(userName)
                .setCreateTime(entity.getCreateTime())
                .setModifyTime(entity.getModifyTime());
        versionMapper.insert(infoVersion);
        return super.updateById(entity);
    }

    @Override
    public SysProductInfoQuery pageByQuery(SysProductInfoQuery query) {
        query.setOrders(OrderItem.descs("CREATE_TIME", "MODIFY_TIME"));
        mapper.pageByQuery(query);
        return query;
    }

    private void check(SysProductInfo entity) {
        if (isExistTypeId(entity)) {
            throw new ServiceException("产品类型重复！");
        }
    }

    private boolean isExistTypeId(SysProductInfo entity) {
        QueryWrapper<SysProductInfo> query = new QueryWrapper<>();
        query.lambda().ne(StrUtil.isNotBlank(entity.getTypeId()), SysProductInfo::getTypeId, entity.getTypeId())
                .eq(SysProductInfo::getTypeId, entity.getTypeId());
        return 0 < mapper.selectCount(query);
    }
}
